package web.protocols.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.entities.Protocolo;
import domain.entities.ProtocoloCompromisopais;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.ProtocolCRUDService;
import services.ProtocoloCompromisopaisService;
import services.exceptions.NoProtocoloCompromisopaisSelectedException;

/**
 * Created with IntelliJ IDEA.
 * User: nico
 * Date: 10/09/12
 * Time: 04:15
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/Protocol/GeneralInformation")
public class GeneralInformationController extends ActionSupport implements ModelDriven{

    private ProtocoloCompromisopais protocoloCompromisopais = new ProtocoloCompromisopais();
    //private ProtocoloCompromisopais protocoloCompromisopaisRecovered;
    private Protocolo protocoloRecovered;
    private ProtocoloCompromisopaisService protocoloCompromisopaisService;
    private ProtocolCRUDService protocolCRUDService;

    @Autowired
    public GeneralInformationController(ProtocoloCompromisopaisService protocoloCompromisopaisService, ProtocolCRUDService protocolCRUDService) {
        this.protocoloCompromisopaisService = protocoloCompromisopaisService;
        this.protocolCRUDService = protocolCRUDService;
    }

    @Action(value="main", results={
            @Result(name="success",location="/protocols.generalinformation.tiles", type="tiles")
    })
    public String execute() throws NoProtocoloCompromisopaisSelectedException {
        //protocoloCompromisopaisRecovered = protocolCRUDService.recoverProtocoloCompromisopais();
        protocoloRecovered = protocolCRUDService.recoverProtocolo();
        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="/protocols.generalinformation.tiles", type="tiles")
    })
    public String save() throws NoProtocoloCompromisopaisSelectedException {
        protocoloCompromisopais.setProtocolo(protocolCRUDService.findById(protocoloCompromisopais.getProtocolo().getId()));
        ProtocoloCompromisopais aux = protocoloCompromisopaisService.save(protocoloCompromisopais);
        protocolCRUDService.setProtocoloCompromisoPais(aux.getProtocolo().getId(), aux.getCompromisopais().getId());
        //protocoloCompromisopaisRecovered = protocolCRUDService.recoverProtocoloCompromisopais();
        protocoloRecovered = protocolCRUDService.recoverProtocolo();
        return SUCCESS;
    }

    @Override
    public Object getModel() {
        return protocoloCompromisopais;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public Protocolo getProtocoloRecovered() {
        return protocoloRecovered;
    }

    public void setProtocoloRecovered(Protocolo protocoloRecovered) {
        this.protocoloRecovered = protocoloRecovered;
    }

    public void setProtocoloCompromisopais(ProtocoloCompromisopais protocoloCompromisopais) {
        this.protocoloCompromisopais = protocoloCompromisopais;
    }
}
